#include <reg52.h>
#include <..\XC51.h>

unsigned char f = 0;
unsigned char Im[4] = {0x00, 0x00, 0x00, 0x00};
unsigned char show[2] = {0, 0};
unsigned long m, Tc;
unsigned char IrOK;

void intersvr1(void) interrupt 2 using 1
{
  Tc = TH0 * 256 + TL0;
  TH0 = 0;
  TL0 = 0;
  if ((Tc > Imin) && (Tc < Imax))
  {
    m = 0;
    f = 1;
    return;
  }

  if (f == 1)
  {
    if (Tc > Inum1 && Tc < Inum3)
    {
      Im[m / 8] = Im[m / 8] >> 1 | 0x80;
      m++;
    }
    if (Tc > Inum2 && Tc < Inum1)
    {
      Im[m / 8] = Im[m / 8] >> 1;
      m++;
    }
    if (m == 32)
    {
      m = 0;
      f = 0;
      if (Im[2] == ~Im[3])
      {
        IrOK = 1;
      }
      else
        IrOK = 0;
    }
  }
}



int main(void)
{
  
  m = 0;
  f = 0;
  IT1 = 1;
  EX1 = 1;
  TMOD = 0x11;
  TH0 = 0;
  TL0 = 0;
  TR0 = 1;
  EA = 1;

  while (1)
  {
		EN1 = EN2 = 1;
    if (IrOK == 1)
    {
      switch (Im[2])
      {
      case 0x46:
        advance();
        break;
      case 0x15:
        back();
        break;
      case 0x43:
        right_turn();
        break;
      case 0x44:
        left_turn();
        break;
      case 0x40:
        stop();
        break;
      default:
        break;
      }
      IrOK = 0;
    }
  }
}
